/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mattespill.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import mattespill.*;
/**
 *
 * @author Erik
 */
public class GUIFortsett extends JPanel {

    private Image bg = new ImageIcon("src/mattespill/gui/images/bakgrunn.png").getImage();
    private ImageIcon croc = new ImageIcon("src/mattespill/gui/images/croc.png");
    private ImageIcon crocopen = new ImageIcon("src/mattespill/gui/images/crocopen.png");
    private ImageIcon chic = new ImageIcon("src/mattespill/gui/images/chicken.png");
    private ImageIcon chichopp = new ImageIcon("src/mattespill/gui/images/chickenhopp.png");
    
    private ImageIcon hscore = new ImageIcon("src/mattespill/gui/images/hscore.png");
    private Oversikt oversikt;
    private Btn krokodille = new Btn(croc);
    private Btn kylling = new Btn(chic);
    private Btn loggut = new Btn("X");
    private Btn nyttSpill = new Btn("Nytt spill");
    private Btn highscore = new Btn(hscore);
    private JPanel panelBgImg;
    private JLabel highscore1 = new JLabel();
    private JLabel highscore2 = new JLabel();
    private JLabel highscore3 = new JLabel();


    public GUIFortsett (Oversikt o){
        this.oversikt = o;
        setPreferredSize(new Dimension(bg.getWidth(null),bg.getHeight(null)+35));
        setLayout(new GridLayout(1,1));
        panelBgImg = new JPanel();
        loggut.addActionListener(new KnappLytter());
        krokodille.addActionListener(new KnappLytter());
        kylling.addActionListener(new KnappLytter());
        nyttSpill.addActionListener(new KnappLytter());
        highscore.addActionListener(new KnappLytter());        
        loggut.setBounds(719, 6, 50, 40);
        if(oversikt.getHighscore().size()>2){
            highscore1 = new JLabel("1: "+oversikt.getHighscore().get(0).getBruker().getNavn().substring(0,oversikt.getHighscore().get(0).getBruker().getNavn().indexOf(" ")));
            highscore2 = new JLabel("2: "+oversikt.getHighscore().get(1).getBruker().getNavn().substring(0,oversikt.getHighscore().get(1).getBruker().getNavn().indexOf(" ")));
            highscore3 = new JLabel("3: "+oversikt.getHighscore().get(2).getBruker().getNavn().substring(0,oversikt.getHighscore().get(2).getBruker().getNavn().indexOf(" ")));
            highscore1.setBounds(245,120,hscore.getIconWidth(),hscore.getIconHeight());
            highscore2.setBounds(242,135,hscore.getIconWidth(),hscore.getIconHeight());
            highscore3.setBounds(239,150,hscore.getIconWidth(),hscore.getIconHeight());
        }else if(oversikt.getHighscore().size() == 1){
            highscore1 = new JLabel("1: "+oversikt.getHighscore().get(0).getBruker().getNavn().substring(0,oversikt.getHighscore().get(0).getBruker().getNavn().indexOf(" ")));
            highscore1.setBounds(245,120,hscore.getIconWidth(),hscore.getIconHeight());
        }else if(oversikt.getHighscore().size() == 2){
            highscore1 = new JLabel("1: "+oversikt.getHighscore().get(0).getBruker().getNavn().substring(0,oversikt.getHighscore().get(0).getBruker().getNavn().indexOf(" ")));
            highscore2 = new JLabel("2: "+oversikt.getHighscore().get(1).getBruker().getNavn().substring(0,oversikt.getHighscore().get(1).getBruker().getNavn().indexOf(" ")));

            highscore1.setBounds(245,120,hscore.getIconWidth(),hscore.getIconHeight());
            highscore2.setBounds(242,135,hscore.getIconWidth(),hscore.getIconHeight());
        }
        kylling.setBounds(660, 370, chic.getIconWidth(), chic.getIconHeight());
        krokodille.setBounds(40, 370,  crocopen.getIconWidth(), crocopen.getIconHeight());
        highscore.setBounds(190,140,hscore.getIconWidth(),hscore.getIconHeight());


        nyttSpill.setBounds(30,75,100,40);
        krokodille.setPressedIcon(crocopen);
        kylling.setPressedIcon(chichopp);
        panelBgImg.setLayout(null);
        panelBgImg.add(loggut);
        panelBgImg.add(kylling);
        panelBgImg.add(nyttSpill);
        panelBgImg.add(krokodille);
        panelBgImg.add(highscore1);
        panelBgImg.add(highscore2);
        panelBgImg.add(highscore3);
        panelBgImg.add(highscore);


        panelBgImg.setBackground(new Color(255,255,255,0));
        add(panelBgImg);
    }
    private class KnappLytter implements ActionListener {
        
        public void actionPerformed(ActionEvent e) {
            JButton hendelse  = (JButton)e.getSource();
            if(hendelse.equals(nyttSpill)){
                oversikt.nyttSpill();
            }
            if(hendelse.equals(krokodille)){
                krokodille.setSize(crocopen.getIconWidth(), crocopen.getIconHeight());
                GUIKrokodille kroko = new GUIKrokodille(oversikt,false);
                sendVidere(kroko);
            }
            if(hendelse.equals(kylling)){
                GUITetris kroko = new GUITetris(oversikt,false);
                sendVidere(kroko);
            }
            if(hendelse.equals(loggut)){
                GUILoggInn l = new GUILoggInn();
                sendVidere(l);
            }
            if(hendelse.equals(highscore)){
                GUIHighscore l = new GUIHighscore(oversikt);
                sendVidere(l);
            }
        }
        private void sendVidere(JPanel j){
            JPanel mainPanel = (JPanel)getParent();
            mainPanel.removeAll(); // dette is the sub-panel
            mainPanel.add(j);
            mainPanel.repaint();
            mainPanel.revalidate();
            j.repaint();
        }
    }
}

class Btn extends JButton{
    
    private Color c = new Color(255,255,255,0);
    
    public Btn(ImageIcon i){
        super(i);
        setSize(new Dimension(i.getIconWidth(),i.getIconHeight()));
        setBackground(c);
        setRolloverEnabled(false);
        setBorder(null);
       //  this.setOpaque(true);
    }
    
    public Btn(String s){
        super(s);
        //this.setBackground(c);
              // this.setOpaque(false);
    }
}
